//
//  KATShapeUtil.m
//  KATFramework
//
//  Created by Kantice on 16/3/20.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATShapeUtil.h"
#import "KATMacros.h"


@implementation KATShapeUtil


//获取形状的通用方法
+ (KATShape *)shapeWithType:(int)type andWidth:(float)width andHeight:(float)height
{
    switch(type)
    {
        //线型
            
        case SHAPE_LINE_H://水平线
            
            return [self lineShapeWithStartPoint:CGPointMake(0, 0) andEndPoint:CGPointMake(width, 0)];
            
            
        case SHAPE_LINE_V://垂直线
            
            return [self lineShapeWithStartPoint:CGPointMake(0, 0) andEndPoint:CGPointMake(0, height)];
            
            
        case SHAPE_LOADING://加载
            
            return [self arcShapeWithRadius:width*0.5 startAngle:M_PI*3.5 endAngle:M_PI*1.75 clockwise:NO];
            
            
        case SHAPE_CHECK://勾
            
            return [self checkShapeWithWidth:width andHeight:height];
            
            
        case SHAPE_CROSS://叉
            
            return [self crossShapeWithWidth:width andHeight:height];
            
            
        case SHAPE_PLUS://加
            
            return [self plusShapeWithWidth:width andHeight:height];
            
        case SHAPE_JUMP://跳跃抛物线
            
            return [self jumpShapeWithStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(width, 0) andHeight:height];
            
            
        case SHAPE_ARROW_U://箭头(上)
            
            return [self arrowShapeWithWidth:width*0.5 height:height length:width*0.32 andDirection:1];
            
            
        case SHAPE_ARROW_D://箭头(下)
            
            return [self arrowShapeWithWidth:width*0.5 height:height length:width*0.32 andDirection:3];
            
            
        case SHAPE_ARROW_L://箭头(左)
            
            return [self arrowShapeWithWidth:width height:height*0.5 length:height*0.32 andDirection:0];
            
            
        case SHAPE_ARROW_R://箭头(右)
            
            return [self arrowShapeWithWidth:width height:height*0.5 length:height*0.32 andDirection:2];
            
            
            
        case SHAPE_DAISY://菊花
            
            return [self daisyShapeWithWidth:width radius:-1 andAmounts:-1];
            
            
        //封闭型
            
        case SHAPE_RECT://矩形
            
            return [self rectShapeWithWidth:width andHeight:height];
            
            
        case SHAPE_RECT_R://圆角矩形
            
            return [self roundedRectShapeWithWidth:width andHeight:height andCornerRadius:(width>height?height*0.25:width*0.25)];
            
            
        case SHAPE_DIAMOND://菱形
            
            return [self diamondShapeWithWidth:width andHeight:height];
            
            
        case SHAPE_OVAL://椭圆形
            
            return [self ovalShapeWithWidth:width andHeight:height];
            
            
        case SHAPE_TRIANGLE://三角形
            
            return [self isoscelesTriangleShapeWithWidth:width andHeight:height];
            
            
        //组合型
            
        case SHAPE_SUCCESS://成功
            
            return [self successShapeWithWidth:width];
            
            
        case SHAPE_FAIL://失败
            
            return [self failShapeWithWidth:width];
            
            break;
            
            
        case SHAPE_ADD://添加
            
            return [self addShapeWithWidth:width];
            
            break;
    }
    
    return nil;
}


//检查是否有该类型
+ (BOOL)hasShapeType:(int)type
{
    BOOL check=NO;
    
    switch(type)
    {
        //线型
        case SHAPE_LINE_H:
        case SHAPE_LINE_V:
        case SHAPE_LOADING:
        case SHAPE_CHECK:
        case SHAPE_CROSS:
        case SHAPE_PLUS:
        case SHAPE_JUMP:
        case SHAPE_ARROW_U:
        case SHAPE_ARROW_D:
        case SHAPE_ARROW_L:
        case SHAPE_ARROW_R:
        case SHAPE_DAISY:
            
        //封闭型
        case SHAPE_RECT:
        case SHAPE_RECT_R:
        case SHAPE_DIAMOND:
        case SHAPE_OVAL:
        case SHAPE_TRIANGLE:
            
        //组合型
        case SHAPE_SUCCESS:
        case SHAPE_FAIL:
        case SHAPE_ADD:
            
            check=YES;
            
            break;
    }
    
    return check;
}

//获取矩形
+ (KATShape *)rectShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[UIBezierPath bezierPathWithRect:shape.bounds];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取圆角矩形
+ (KATShape *)roundedRectShapeWithWidth:(float)width andHeight:(float)height andCornerRadius:(float)radius
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:shape.bounds cornerRadius:radius];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取菱形
+ (KATShape *)diamondShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(0, height*0.5)];
    [path addLineToPoint:CGPointMake(width*0.5, 0)];
    [path addLineToPoint:CGPointMake(width, height*0.5)];
    [path addLineToPoint:CGPointMake(width*0.5, height)];
    [path closePath];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取圆形
+ (KATShape *)roundShapeWithRadius:(float)radius
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, radius*2, radius*2);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:shape.bounds];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取椭圆
+ (KATShape *)ovalShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, width, height)];
    
    shape.path=path.CGPath;
    
    return shape;
}

//获取等腰三角形
+ (KATShape *)isoscelesTriangleShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(0, height)];
    [path addLineToPoint:CGPointMake(width*0.5, 0)];
    [path addLineToPoint:CGPointMake(width, height)];
    [path closePath];
    
    shape.path=path.CGPath;
    
    return shape;
}

//获取等边三角形
+ (KATShape *)equilateralTriangleShapeWithLength:(float)length
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, length, length*sqrtf(3.0)/2.0);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(0, shape.bounds.size.height)];
    [path addLineToPoint:CGPointMake(shape.bounds.size.width*0.5, 0)];
    [path addLineToPoint:CGPointMake(shape.bounds.size.width, shape.bounds.size.height)];
    [path closePath];
    
    shape.path=path.CGPath;
    
    return shape;
}

//获取三角形
+ (KATShape *)triangleShapeWithPointA:(CGPoint)pa andPointB:(CGPoint)pb andPointC:(CGPoint)pc
{
    float minX=CGFLOAT_MAX;
    float minY=CGFLOAT_MAX;
    float maxX=CGFLOAT_MIN;
    float maxY=CGFLOAT_MIN;
    
    if(pa.x>maxX)
    {
        maxX=pa.x;
    }
    
    if(pb.x>maxX)
    {
        maxX=pb.x;
    }
    
    if(pc.x>maxX)
    {
        maxX=pc.x;
    }
    
    if(pa.y>maxY)
    {
        maxY=pa.y;
    }
    
    if(pb.y>maxY)
    {
        maxY=pb.y;
    }
    
    if(pc.y>maxY)
    {
        maxY=pc.y;
    }
    
    if(pa.x<minX)
    {
        minX=pa.x;
    }
    
    if(pb.x<minX)
    {
        minX=pb.x;
    }
    
    if(pc.x<minX)
    {
        minX=pc.x;
    }
    
    if(pa.y<minY)
    {
        minY=pa.y;
    }
    
    if(pb.y<minY)
    {
        minY=pb.y;
    }
    
    if(pc.y<minY)
    {
        minY=pc.y;
    }
    
    
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, maxX-minX, maxY-minY);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(pa.x-minX, pa.y-minY)];
    [path addLineToPoint:CGPointMake(pb.x-minX, pb.y-minY)];
    [path addLineToPoint:CGPointMake(pc.x-minX, pc.y-minY)];
    [path closePath];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取扇形
+ (KATShape *)sectorShapeWithRadius:(float)radius width:(float)width startAngle:(float)startAngle endAngle:(float)endAngle clockwise:(BOOL)clockwise
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, radius*2, radius*2);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake((radius+cos(startAngle)*(radius-width)), (radius+sin(startAngle)*(radius-width)))];
    [path addLineToPoint:CGPointMake((radius+cos(startAngle)*(radius-width)), (radius+sin(startAngle)*(radius-width)))];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];
    [path addLineToPoint:CGPointMake((radius+cos(endAngle)*(radius-width)), (radius+sin(endAngle)*(radius-width)))];
    [path addArcWithCenter:CGPointMake(radius, radius) radius:radius-width startAngle:endAngle endAngle:startAngle clockwise:!clockwise];
    [path closePath];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取圆弧
+ (KATShape *)arcShapeWithRadius:(float)radius startAngle:(float)startAngle endAngle:(float)endAngle clockwise:(BOOL)clockwise
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, radius*2, radius*2);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取直线
+ (KATShape *)lineShapeWithStartPoint:(CGPoint)startPoint andEndPoint:(CGPoint)endPoint
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, startPoint.x>=endPoint.x?startPoint.x:endPoint.x, startPoint.y>=endPoint.y?startPoint.y:endPoint.y);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:startPoint];
    [path addLineToPoint:endPoint];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取三点线型
+ (KATShape *)lineShapeWithPointA:(CGPoint)pa pointB:(CGPoint)pb pointC:(CGPoint)pc
{
    KATFloatArray *px=[KATFloatArray floatArrayWithCapacity:10];
    KATFloatArray *py=[KATFloatArray floatArrayWithCapacity:10];
    
    [px put:pa.x];
    [px put:pb.x];
    [px put:pc.x];
    
    [py put:pa.y];
    [py put:pb.y];
    [py put:pc.y];
    
    
    KATShape *shape=[KATShape shape];
//    shape.bounds=CGRectMake(0, 0, px.max, py.max);
    shape.frame=CGRectMake(px.min, py.min, px.max-px.min, py.max-py.min);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:pa];
    [path addLineToPoint:pb];
    [path addLineToPoint:pc];
    
    shape.path=path.CGPath;
    
    return shape;
}

//获取四点线型
+ (KATShape *)lineShapeWithPointA:(CGPoint)pa pointB:(CGPoint)pb pointC:(CGPoint)pc pointD:(CGPoint)pd
{
    KATFloatArray *px=[KATFloatArray floatArrayWithCapacity:10];
    KATFloatArray *py=[KATFloatArray floatArrayWithCapacity:10];
    
    [px put:pa.x];
    [px put:pb.x];
    [px put:pc.x];
    [px put:pd.x];
    
    [py put:pa.y];
    [py put:pb.y];
    [py put:pc.y];
    [py put:pd.y];
    
    
    KATShape *shape=[KATShape shape];
//    shape.bounds=CGRectMake(0, 0, px.max, py.max);
    shape.frame=CGRectMake(px.min, py.min, px.max-px.min, py.max-py.min);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:pa];
    [path addLineToPoint:pb];
    [path addLineToPoint:pc];
    [path addLineToPoint:pd];
    
    shape.path=path.CGPath;
    
    return shape;
}

//获取五点线型
+ (KATShape *)lineShapeWithPointA:(CGPoint)pa pointB:(CGPoint)pb pointC:(CGPoint)pc pointD:(CGPoint)pd pointE:(CGPoint)pe
{
    KATFloatArray *px=[KATFloatArray floatArrayWithCapacity:10];
    KATFloatArray *py=[KATFloatArray floatArrayWithCapacity:10];
    
    [px put:pa.x];
    [px put:pb.x];
    [px put:pc.x];
    [px put:pd.x];
    [px put:pe.x];
    
    [py put:pa.y];
    [py put:pb.y];
    [py put:pc.y];
    [py put:pd.y];
    [py put:pe.y];
    
    
    KATShape *shape=[KATShape shape];
//    shape.bounds=CGRectMake(0, 0, px.max, py.max);
    shape.frame=CGRectMake(px.min, py.min, px.max-px.min, py.max-py.min);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:pa];
    [path addLineToPoint:pb];
    [path addLineToPoint:pc];
    [path addLineToPoint:pd];
    [path addLineToPoint:pe];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取N点线型
+ (KATShape *)lineShapeWithPoints:(KATArray *)points andClosed:(BOOL)closed
{
    if(points && points.length>1)
    {
        KATFloatArray *px=[KATFloatArray floatArrayWithCapacity:(int)points.length];
        KATFloatArray *py=[KATFloatArray floatArrayWithCapacity:(int)points.length];
        
        for(int i=0;i<points.length;i++)
        {
            CGPoint p=[points[i] CGPointValue];
            
            [px put:p.x];
            [py put:p.y];
        }
        
        
        KATShape *shape=[KATShape shape];
//        shape.bounds=CGRectMake(0, 0, px.max, py.max);
        shape.frame=CGRectMake(px.min, py.min, px.max-px.min, py.max-py.min);
        shape.fillColor=[UIColor clearColor].CGColor;
        shape.strokeColor=KCOLOR_GRAY.CGColor;
        shape.lineJoin=kCALineJoinRound;
        shape.lineCap=kCALineCapRound;
        shape.lineWidth=2.0f;
        
        UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
        
        [path moveToPoint:[[points firstMember] CGPointValue]];
        
        for(int i=1;i<points.length;i++)
        {
            CGPoint p=[points[i] CGPointValue];
            
            [path addLineToPoint:p];
        }
        
        //是否闭合
        if(closed)
        {
            [path closePath];
        }
        
        shape.path=path.CGPath;
        
        return shape;
    }
    
    return nil;
}


//获取N点平滑曲线(平滑度，0.382~0.618之间比较合适)
+ (KATShape *)curveShapeWithPoints:(KATArray *)points andSmooth:(float)smooth closed:(BOOL)closed
{
    if(points && points.length>1)
    {
        KATFloatArray *px=[KATFloatArray floatArrayWithCapacity:(int)points.length];
        KATFloatArray *py=[KATFloatArray floatArrayWithCapacity:(int)points.length];
        
        KATArray *midPoints=[KATArray arrayWithCapacity:points.length];//中心点（参考点）
        
        for(int i=0;i<points.length;i++)
        {
            CGPoint p=[points[i] CGPointValue];
            
            [px put:p.x];
            [py put:p.y];
            
            if(i==points.length-1)//最后一个点
            {
                if(closed)//是否闭合
                {
                    CGPoint next=[points[0] CGPointValue];//下一个点(闭合点的下一个点为0点)
                    
                    CGPoint mp=CGPointMake((p.x+next.x)/2.0, (p.y+next.y)/2.0);//计算出中点
                    
                    [midPoints put:[NSValue valueWithCGPoint:mp]];
                }
            }
            else//普通点
            {
                CGPoint next=[points[i+1] CGPointValue];//下一个点
                
                CGPoint mp=CGPointMake((p.x+next.x)/2.0, (p.y+next.y)/2.0);//计算出中点
                
                [midPoints put:[NSValue valueWithCGPoint:mp]];
            }
        }
        
        //控制点数组
        KATArray *controlPoints0=[KATArray arrayWithCapacity:points.length];
        KATArray *controlPoints1=[KATArray arrayWithCapacity:points.length];
        
        //平移中点，计算控制点
        for(int i=0;i<points.length;i++)
        {
            if(i==points.length-1)//最后一个点
            {
                if(closed)//是否闭合
                {
                    CGPoint opp=[points[i-1] CGPointValue];//原点前一点
                    CGPoint op=[points[i] CGPointValue];//原点(要计算控制点的点)
                    CGPoint onp=[points[0] CGPointValue];//原点后一点
                    
                    CGPoint mpp=[midPoints[i-1] CGPointValue];//原点前的中点
                    CGPoint mnp=[midPoints[i] CGPointValue];//原点后的中点
                    
                    double pd=[KATMath distanceWithPointA:op andPointB:opp];//原点与前点的距离
                    double nd=[KATMath distanceWithPointA:onp andPointB:op];//原点与后点的距离
                    
                    //平移参考点
                    CGPoint bp=CGPointMake(pd+nd>0?(pd*mnp.x+nd*mpp.x)/(pd+nd):mnp.x, pd+nd>0?(pd*mnp.y+nd*mpp.y)/(pd+nd):mnp.y);
                    
                    //平移后的控制点
                    CGPoint cp0=CGPointMake(mpp.x+(op.x-bp.x), mpp.y+(op.y-bp.y));
                    CGPoint cp1=CGPointMake(mnp.x+(op.x-bp.x), mnp.y+(op.y-bp.y));
                    
                    //平滑度调整
                    cp0=CGPointMake(cp0.x+(op.x-cp0.x)*(1.0-smooth), cp0.y+(op.y-cp0.y)*(1.0-smooth));
                    cp1=CGPointMake(cp1.x+(op.x-cp1.x)*(1.0-smooth), cp1.y+(op.y-cp1.y)*(1.0-smooth));
                    
                    //存入数组
                    [controlPoints0 put:[NSValue valueWithCGPoint:cp0]];
                    [controlPoints1 put:[NSValue valueWithCGPoint:cp1]];
                }
                else
                {
                    //无控制点
                    
                    //存入数组
                    [controlPoints0 put:[NSValue valueWithCGPoint:[points[i] CGPointValue]]];
                    [controlPoints1 put:[NSValue valueWithCGPoint:[points[i] CGPointValue]]];
                }
            }
            else if(i==0)//首个点
            {
                if(closed)//是否闭合
                {
                    CGPoint opp=[points[points.length-1] CGPointValue];//原点前一点
                    CGPoint op=[points[i] CGPointValue];//原点(要计算控制点的点)
                    CGPoint onp=[points[i+1] CGPointValue];//原点后一点
                    
                    CGPoint mpp=[midPoints[midPoints.length-1] CGPointValue];//原点前的中点
                    CGPoint mnp=[midPoints[i] CGPointValue];//原点后的中点
                    
                    double pd=[KATMath distanceWithPointA:op andPointB:opp];//原点与前点的距离
                    double nd=[KATMath distanceWithPointA:onp andPointB:op];//原点与后点的距离
                    
                    //平移参考点
                    CGPoint bp=CGPointMake(pd+nd>0?(pd*mnp.x+nd*mpp.x)/(pd+nd):mnp.x, pd+nd>0?(pd*mnp.y+nd*mpp.y)/(pd+nd):mnp.y);
                    
                    //平移后的控制点
                    CGPoint cp0=CGPointMake(mpp.x+(op.x-bp.x), mpp.y+(op.y-bp.y));
                    CGPoint cp1=CGPointMake(mnp.x+(op.x-bp.x), mnp.y+(op.y-bp.y));
                    
                    //平滑度调整
                    cp0=CGPointMake(cp0.x+(op.x-cp0.x)*(1.0-smooth), cp0.y+(op.y-cp0.y)*(1.0-smooth));
                    cp1=CGPointMake(cp1.x+(op.x-cp1.x)*(1.0-smooth), cp1.y+(op.y-cp1.y)*(1.0-smooth));
                    
                    //存入数组
                    [controlPoints0 put:[NSValue valueWithCGPoint:cp0]];
                    [controlPoints1 put:[NSValue valueWithCGPoint:cp1]];
                }
                else
                {
                    //无控制点
                    
                    //存入数组
                    [controlPoints0 put:[NSValue valueWithCGPoint:[points[i] CGPointValue]]];
                    [controlPoints1 put:[NSValue valueWithCGPoint:[points[i] CGPointValue]]];
                }
            }
            else//普通点
            {
                CGPoint opp=[points[i-1] CGPointValue];//原点前一点
                CGPoint op=[points[i] CGPointValue];//原点(要计算控制点的点)
                CGPoint onp=[points[i+1] CGPointValue];//原点后一点
                
                CGPoint mpp=[midPoints[i-1] CGPointValue];//原点前的中点
                CGPoint mnp=[midPoints[i] CGPointValue];//原点后的中点
                
                double pd=[KATMath distanceWithPointA:op andPointB:opp];//原点与前点的距离
                double nd=[KATMath distanceWithPointA:onp andPointB:op];//原点与后点的距离
                
                //平移参考点
                CGPoint bp=CGPointMake(pd+nd>0?(pd*mnp.x+nd*mpp.x)/(pd+nd):mnp.x, pd+nd>0?(pd*mnp.y+nd*mpp.y)/(pd+nd):mnp.y);
                
                //平移后的控制点
                CGPoint cp0=CGPointMake(mpp.x+(op.x-bp.x), mpp.y+(op.y-bp.y));
                CGPoint cp1=CGPointMake(mnp.x+(op.x-bp.x), mnp.y+(op.y-bp.y));
                
                //平滑度调整
                cp0=CGPointMake(cp0.x+(op.x-cp0.x)*(1.0-smooth), cp0.y+(op.y-cp0.y)*(1.0-smooth));
                cp1=CGPointMake(cp1.x+(op.x-cp1.x)*(1.0-smooth), cp1.y+(op.y-cp1.y)*(1.0-smooth));
                
                //存入数组
                [controlPoints0 put:[NSValue valueWithCGPoint:cp0]];
                [controlPoints1 put:[NSValue valueWithCGPoint:cp1]];
            }
        }
        
        
        KATShape *shape=[KATShape shape];
//        shape.bounds=CGRectMake(0, 0, px.max, py.max);
        shape.frame=CGRectMake(px.min, py.min, px.max-px.min, py.max-py.min);
        shape.fillColor=[UIColor clearColor].CGColor;
        shape.strokeColor=KCOLOR_GRAY.CGColor;
        shape.lineJoin=kCALineJoinRound;
        shape.lineCap=kCALineCapRound;
        shape.lineWidth=2.0f;
        
        UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
        
        [path moveToPoint:[[points firstMember] CGPointValue]];
        
        for(int i=1;i<points.length;i++)
        {
            CGPoint p=[points[i] CGPointValue];
            CGPoint cp0=[controlPoints1[i-1] CGPointValue];
            CGPoint cp1=[controlPoints0[i] CGPointValue];
            
            [path addCurveToPoint:p controlPoint1:cp0 controlPoint2:cp1];
        }
        
        if(closed)//闭合
        {
            CGPoint p=[points[0] CGPointValue];
            CGPoint cp0=[controlPoints1[points.length-1] CGPointValue];
            CGPoint cp1=[controlPoints0[0] CGPointValue];
            
            [path addCurveToPoint:p controlPoint1:cp0 controlPoint2:cp1];
        }
        
        //是否闭合
        if(closed)
        {
            [path closePath];
        }
        
        shape.path=path.CGPath;
        
        return shape;
    }
    
    return nil;
}


//获取勾
+ (KATShape *)checkShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(0, height*0.667)];
    [path addLineToPoint:CGPointMake(width*0.333, height)];
    [path addLineToPoint:CGPointMake(width, height*0.333)];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取叉
+ (KATShape *)crossShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(width, height)];
    
    [path moveToPoint:CGPointMake(width, 0)];
    [path addLineToPoint:CGPointMake(0, height)];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取加号
+ (KATShape *)plusShapeWithWidth:(float)width andHeight:(float)height
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(0, height*0.5)];
    [path addLineToPoint:CGPointMake(width, height*0.5)];
    
    [path moveToPoint:CGPointMake(width*0.5, 0)];
    [path addLineToPoint:CGPointMake(width*0.5, height)];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取切圆形
+ (KATShape *)cutRoundShapeWithRadius:(float)radius startAngle:(float)startAngle endAngle:(float)endAngle clockwise:(BOOL)clockwise;
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, radius*2, radius*2);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];
    [path closePath];
    
    shape.path=path.CGPath;
    
    return shape;
}


///获取气泡(尺寸、圆角半径、箭头点、箭头宽度、箭头高度)(方向:0左、1上、2右、3下)
+ (KATShape *)bubbleShapeWithSize:(CGSize)size cornerRadius:(float)radius andArrowPoint:(CGPoint)point width:(float)width height:(float)height direction:(int)direction
{
    if(direction<0)
    {
        direction=0;
    }
    
    if(direction>3)
    {
        direction=3;
    }
    
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, size.width, size.height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:point];
    
    if(direction==0)//左
    {
        [path addLineToPoint:CGPointMake(point.x+height, (point.y-width*0.5>=radius?point.y-width*0.5:radius))];
        [path addLineToPoint:CGPointMake(point.x+height, radius)];
        [path addArcWithCenter:CGPointMake(point.x+height+radius, radius) radius:radius startAngle:M_PI*1 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(size.width-radius, 0)];
        [path addArcWithCenter:CGPointMake(size.width-radius, radius) radius:radius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(size.width, size.height-radius)];
        [path addArcWithCenter:CGPointMake(size.width-radius, size.height-radius) radius:radius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(point.x+height+radius, size.height)];
        [path addArcWithCenter:CGPointMake(point.x+height+radius, size.height-radius) radius:radius startAngle:M_PI*0.5 endAngle:M_PI*1 clockwise:YES];
        [path addLineToPoint:CGPointMake(point.x+height, (point.y+width*0.5<=size.height-radius?point.y+width*0.5:size.height-radius))];
        [path closePath];
    }
    else if(direction==1)//上
    {
        [path addLineToPoint:CGPointMake((point.x+width*0.5<=size.width-radius?point.x+width*0.5:size.width-radius), point.y+height)];
        [path addLineToPoint:CGPointMake(size.width-radius, point.y+height)];
        [path addArcWithCenter:CGPointMake(size.width-radius, point.y+height+radius) radius:radius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(size.width, size.height-radius)];
        [path addArcWithCenter:CGPointMake(size.width-radius, size.height-radius) radius:radius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(radius, size.height)];
        [path addArcWithCenter:CGPointMake(radius, size.height-radius) radius:radius startAngle:M_PI*0.5 endAngle:M_PI*1 clockwise:YES];
        [path addLineToPoint:CGPointMake(0, point.y+height+radius)];
        [path addArcWithCenter:CGPointMake(radius, point.y+height+radius) radius:radius startAngle:M_PI*1 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake((point.x-width*0.5>radius?point.x-width*0.5:radius), point.y+height)];
        [path closePath];
    }
    else if(direction==2)//右
    {
        [path addLineToPoint:CGPointMake(point.x-height, (point.y+width*0.5<=size.height-radius?point.y+width*0.5:size.height-radius))];
        [path addLineToPoint:CGPointMake(point.x-height, size.height-radius)];
        [path addArcWithCenter:CGPointMake(point.x-height-radius, size.height-radius) radius:radius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(radius, size.height)];
        [path addArcWithCenter:CGPointMake(radius, size.height-radius) radius:radius startAngle:M_PI*0.5 endAngle:M_PI*1 clockwise:YES];
        [path addLineToPoint:CGPointMake(0, radius)];
        [path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:M_PI*1 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(point.x-height-radius, 0)];
        [path addArcWithCenter:CGPointMake(point.x-height-radius, radius) radius:radius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(point.x-height, (point.y-width*0.5>=radius?point.y-width*0.5:radius))];
        [path closePath];
    }
    else if(direction==3)//下
    {
        [path addLineToPoint:CGPointMake((point.x-width*0.5>=radius?point.x-width*0.5:radius), point.y-height)];
        [path addLineToPoint:CGPointMake(radius, point.y-height)];
        [path addArcWithCenter:CGPointMake(radius, point.y-height-radius) radius:radius startAngle:M_PI*0.5 endAngle:M_PI*1 clockwise:YES];
        [path addLineToPoint:CGPointMake(0, radius)];
        [path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:M_PI*1 endAngle:M_PI*1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(size.width-radius, 0)];
        [path addArcWithCenter:CGPointMake(size.width-radius, radius) radius:radius startAngle:M_PI*1.5 endAngle:M_PI*2.0 clockwise:YES];
        [path addLineToPoint:CGPointMake(size.width, point.y-height-radius)];
        [path addArcWithCenter:CGPointMake(size.width-radius, point.y-height-radius) radius:radius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
        [path addLineToPoint:CGPointMake((point.x+width*0.5<=size.width-radius?point.x+width*0.5:size.width-radius), point.y-height)];
        [path closePath];
    }
    
    shape.path=path.CGPath;
    
    return shape;
}


///获取跳跃抛物线轨迹
+ (KATShape *)jumpShapeWithStartPoint:(CGPoint)sp endPoint:(CGPoint)ep andHeight:(float)height
{
    //抛物线顶点式方程式y=a(x-h)^2+k (k为顶点高度)
    
    //转换坐标
    float x1=ep.x-sp.x;
    float y1=sp.y-ep.y;
    
    //判断高度是否为零或低于y2
    if(y1>height)
    {
        height=y1;
    }
    
    if(height==0.0)
    {
        return [self lineShapeWithStartPoint:sp andEndPoint:ep];
    }
    
    //计算
    float k=height;
    float h=x1/(sqrtf(1.0-y1/k)+1.0);
    
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, fabsf(x1), k+(y1>=0?0:-y1));
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(sp.x+(x1>=0?0:-x1), sp.y+k)];
    [path addQuadCurveToPoint:CGPointMake(ep.x+(x1>=0?0:-x1), ep.y+k) controlPoint:CGPointMake(sp.x+h+(x1>=0?0:-x1), sp.y+k-k-(k-y1/2.0))];

    shape.path=path.CGPath;
    
    return shape;
    
    return nil;
}


//获取箭头(从上下箭头的角度看:高度为整体高度，宽度为箭头的宽度;左右箭头则相反，长度为箭头的余弦长)(方向:0左、1上、2右、3下)
+ (KATShape *)arrowShapeWithWidth:(float)width height:(float)height length:(float)length andDirection:(int)direction;
{
    if(direction<0)
    {
        direction=0;
    }
    
    if(direction>3)
    {
        direction=3;
    }
    
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, height);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    if(direction==0)//左
    {
        [path moveToPoint:CGPointMake(width, height*0.5)];
        [path addLineToPoint:CGPointMake(0, height*0.5)];
        [path moveToPoint:CGPointMake(length, 0)];
        [path addLineToPoint:CGPointMake(0, height*0.5)];
        [path addLineToPoint:CGPointMake(length, height)];
    }
    else if(direction==1)//上
    {
        [path moveToPoint:CGPointMake(width*0.5, height)];
        [path addLineToPoint:CGPointMake(width*0.5, 0)];
        [path moveToPoint:CGPointMake(0, length)];
        [path addLineToPoint:CGPointMake(width*0.5, 0)];
        [path addLineToPoint:CGPointMake(width, length)];
    }
    else if(direction==2)//右
    {
        [path moveToPoint:CGPointMake(0, height*0.5)];
        [path addLineToPoint:CGPointMake(width, height*0.5)];
        [path moveToPoint:CGPointMake(width-length, 0)];
        [path addLineToPoint:CGPointMake(width, height*0.5)];
        [path addLineToPoint:CGPointMake(width-length, height)];
    }
    else if(direction==3)//下
    {
        [path moveToPoint:CGPointMake(width*0.5, 0)];
        [path addLineToPoint:CGPointMake(width*0.5, height)];
        [path moveToPoint:CGPointMake(0, height-length)];
        [path addLineToPoint:CGPointMake(width*0.5, height)];
        [path addLineToPoint:CGPointMake(width, height-length)];
    }
    
    shape.path=path.CGPath;
    
    return shape;
}



//获取菊花(宽度、内径、花瓣数)
+ (KATShape *)daisyShapeWithWidth:(float)width radius:(float)radius andAmounts:(int)amounts
{
    if(radius<0 || radius>width*0.5)
    {
        radius=width*0.5*0.618;
    }
    
    if(amounts<=0 || amounts>360)
    {
        amounts=16;
    }
    
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, width);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    for(int i=0;i<amounts;i++)
    {
        [path moveToPoint:CGPointMake(width*0.5+cos(M_PI*1.5+M_PI*2.0/amounts*i)*radius, width*0.5+sin(M_PI*1.5+M_PI*2.0/amounts*i)*radius)];
        [path addLineToPoint:CGPointMake(width*0.5+cos(M_PI*1.5+M_PI*2.0/amounts*i)*width*0.5, width*0.5+sin(M_PI*1.5+M_PI*2.0/amounts*i)*width*0.5)];
    }
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取成功形状(圆圈＋勾)
+ (KATShape *)successShapeWithWidth:(float)width
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, width);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path addArcWithCenter:CGPointMake(width*0.5, width*0.5) radius:width*0.5 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
    [path moveToPoint:CGPointMake(width*0.19, width*0.19+width*0.62*0.667)];
    [path addLineToPoint:CGPointMake(width*0.19+width*0.62*0.333, width*0.19+width*0.62)];
    [path addLineToPoint:CGPointMake(width*0.19+width*0.62, width*0.19+width*0.62*0.333)];
    
    shape.path=path.CGPath;
    
    return shape;
}


//获取失败形状(圆圈＋叉)
+ (KATShape *)failShapeWithWidth:(float)width
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, width);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path addArcWithCenter:CGPointMake(width*0.5, width*0.5) radius:width*0.5 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
    
    [path moveToPoint:CGPointMake(width*0.25, width*0.25)];
    [path addLineToPoint:CGPointMake(width*0.25+width*0.5, width*0.25+width*0.5)];
    
    [path moveToPoint:CGPointMake(width*0.25+width*0.5, width*0.25)];
    [path addLineToPoint:CGPointMake(width*0.25, width*0.25+width*0.5)];
    
    shape.path=path.CGPath;
    
    return shape;
}


///获取添加形状(圆圈＋加号)
+ (KATShape *)addShapeWithWidth:(float)width
{
    KATShape *shape=[KATShape shape];
    shape.bounds=CGRectMake(0, 0, width, width);
    shape.fillColor=[UIColor clearColor].CGColor;
    shape.strokeColor=KCOLOR_GRAY.CGColor;
    shape.lineJoin=kCALineJoinRound;
    shape.lineCap=kCALineCapRound;
    shape.lineWidth=2.0f;
    
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path addArcWithCenter:CGPointMake(width*0.5, width*0.5) radius:width*0.5 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
    
    [path moveToPoint:CGPointMake(width*0.2, width*0.5)];
    [path addLineToPoint:CGPointMake(width*0.8, width*0.5)];
    
    [path moveToPoint:CGPointMake(width*0.5, width*0.2)];
    [path addLineToPoint:CGPointMake(width*0.5, width*0.8)];
    
    shape.path=path.CGPath;
    
    return shape;

}


@end

